package com.ithinkstudio.jeez;

import com.ithinkstudio.jeez.entity.User;
import com.ithinkstudio.jeez.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 启动类
 * @author felix
 */
@SpringBootApplication
public class SpringbootCommandLineApplication implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpringbootCommandLineApplication.class);

    @Resource
    private IUserService userService ;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootCommandLineApplication.class, args);
    }

    //@Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            System.out.println("Let's inspect the beans provided by Spring Boot:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
        };
    }


    /**
     * Callback used to run the bean.
     *
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    @Override
    public void run(String... args) throws Exception {
        //入口逻辑
        LOGGER.info("程序启动！");
        try {
            User user = userService.findById(1);
            LOGGER.info("{}", user);
        } catch (Exception e) {
            LOGGER.error("程序异常: {}", e.getMessage());
        }
        LOGGER.info("程序结束！");
    }
}
